package com.yunyi.integration.config;

import lombok.extern.log4j.Log4j;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.session.SqlSession;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.sql.Connection;

/**
 * 获取myBatis数据源
 *
 * @author yunyi
 */
@Component
@Slf4j
public class GetConnection {

    @Autowired
    private SqlSession sqlSession;

    /**
     * 获取数据库连接
     *
     * @return 数据库连接
     */
    public Connection getConnection() {
        Connection conn = null;
        try {
            conn = sqlSession.getConfiguration().getEnvironment().getDataSource().getConnection();
            log.info("===This Connection isClosed ? " + conn.isClosed());
        } catch (Exception e) {
            e.printStackTrace();
        }
        return conn;
    }


}
